using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CSTControls
{
    public enum bordersides
    { 
        All,
        Left,
        Right,
        Top,
        Bottom,
        None
    }
    public partial class cstTextBox : UserControl
    {
        public string currentText;
        public delegate void TextChangedHandler(object cstTextBox,EventArgs e);
        public event TextChangedHandler OnTextChange;
        private float _borderSize = 1;
        public float borderSize
        {
            get
            {
                return _borderSize;
            }
            set
            {
                _borderSize = value;
            }
        }

        private bordersides _borderSide;
        public bordersides borderSide
        {
            get
            {
                return _borderSide;
            }
            set
            {
                _borderSide = value;
            }
        }

        public cstTextBox()
        {
            InitializeComponent();
            baseTextBox.Text = "";
            
        }
        protected override void OnGotFocus(EventArgs e)
        {
                base.OnGotFocus(e);
                      
        }
        protected override void OnLostFocus(EventArgs e)
        {
                base.OnLostFocus(e);
           
        }
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
        }
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
           
        }
        /// <summary>
        /// Gets the length of text in the control.(Inherited from 
        /// </summary>
        public int TextLength
        {
            get
            {
                return baseTextBox.TextLength;
            }
           
        }
        /// <summary>
        /// Returns the maximum number of characters allowed in the text box. The default value is 0, meaning that there is no limit on the length.
        /// </summary>
        public int MaxLength
        {
            get
            {
                return baseTextBox.MaxLength;
            }
            set
            {
                baseTextBox.MaxLength = value;
            }
        }
        /// <summary>
        /// Gets or sets how text is aligned in a TextBox control.
        /// </summary>
        public HorizontalAlignment TextAlign
        {
            get
            {
                return baseTextBox.TextAlign;
            }
            set
            {
                baseTextBox.TextAlign = value;
            }
        }
        /// <summary>
        /// Gets or sets the current text in the TextBox.
        /// </summary>
        public override string Text 
        {
            get
            {
                return baseTextBox.Text;
            }
            set
            {
                baseTextBox.Text = value;
                
            }
        }
        /// <summary>
        /// Gets or sets a value indicating the currently selected text in the control
        /// </summary>
        public  string SelectedText
        {
            get
            {
                return(baseTextBox.SelectedText);
            }
            set
            {
                baseTextBox.SelectedText = value;
            }
        }
        /// <summary>
        /// Gets or sets the number of characters selected in the text box.
        /// </summary>
        public  int SelectionLength
        {
            get
            {
                return (baseTextBox.SelectionLength);
            }
            set
            {
                baseTextBox.SelectionLength = value;
            }
        }
        /// <summary>
        /// Gets or sets the starting point of text selected in the text box.
        /// </summary>
        public int Selectionstart
        {
            get
            {
                return (baseTextBox.SelectionStart);
            }
            set
            {
                baseTextBox.SelectionStart = value;
            }
        }
        /// <summary>
        /// Gets or sets the specified background color for the style.
        /// </summary>
        public override Color BackColor
        {
            get
            {
                return baseTextBox.BackColor;
            }
            set
            {
                baseTextBox.BackColor = value;
            }
        }
        /// <summary>
        /// Gets or sets the specified foreground color for the style. Typically, this property sets the color for the text. The default value is Empty.
        /// </summary>
        public override Color ForeColor
        {
            get
            {
                return baseTextBox.ForeColor;
            }
            set
            {
                baseTextBox.ForeColor = value;
            }
        }
        /// <summary>
        /// Selects all text in the text box
        /// </summary>
        public void SelectAll()
        {
            baseTextBox.SelectAll();
        }
        /// <summary>
        /// Gets a value indicating whether the user can undo the previous operation in a text box control
        /// </summary>
        public bool CanUndo
        {
            get
            {
                return (baseTextBox.CanUndo);
            }
           
        }
        /// <summary>
        /// Get the property whether the control has focus or not.
        /// </summary>
        public override bool Focused
        {
            get
            {
                return (baseTextBox.Focused);
            }
        }
        /// <summary>
        /// /Undoes the last edit operation in the text box. 
        /// </summary>
        public void Undo()
        {
            baseTextBox.Undo();
        }
        
        private void cstTextBox_Paint(object sender, PaintEventArgs e)
        {
            Pen tPen ;
            Graphics gLine ;
             
            gLine= e.Graphics;
            baseTextBox.Font = this.Font;
            baseTextBox.Location = new Point(Convert.ToInt16 (_borderSize),Convert .ToInt16 ( _borderSize));
            baseTextBox.Width = this.Width - 2 * Convert.ToInt16 (_borderSize);
            baseTextBox.Height = this.Height - 2 * Convert.ToInt16 (_borderSize);
            tPen = new Pen(Color.Black ,(float)_borderSize);
            float cordX1 = _borderSize/2;
            float cordX2 = this.Width-_borderSize/2-1.5f ;
            float cordY1 = _borderSize / 2;
            float cordY2 = this.Height -_borderSize/2 -1.5f;
            if ((_borderSize % 2)!=0)
            {
                cordX2++; cordY2++;
            }
            switch (borderSide )
            {
                case bordersides.All:
                    gLine.DrawLine(tPen, cordX1, cordY1 - _borderSize / 2, cordX1, cordY2 + _borderSize / 2);
                    gLine.DrawLine(tPen, cordX2, cordY1 - _borderSize / 2, cordX2, cordY2 + _borderSize / 2);
                    gLine.DrawLine(tPen, cordX1 - _borderSize / 2, cordY1, cordX2 + _borderSize / 2, cordY1);
                    gLine.DrawLine(tPen, cordX1 - _borderSize / 2, cordY2, cordX2 + _borderSize / 2, cordY2);
                    break;
                case bordersides.Left :
                    gLine.DrawLine(tPen, cordX1, cordY1 - _borderSize /2, cordX1, cordY2 + _borderSize /2);
                    break;
                case bordersides.Right  :
                    gLine.DrawLine(tPen, cordX2, cordY1 - _borderSize/2, cordX2, cordY2 + _borderSize/2);
                    break;
                case bordersides.Top  :
                    gLine.DrawLine(tPen, cordX1 - _borderSize /2, cordY1, cordX2 + _borderSize /2, cordY1);
                    break;
                case bordersides.Bottom:
                    gLine.DrawLine(tPen, cordX1-_borderSize/2, cordY2, cordX2+_borderSize/2, cordY2);
                    break;
                case bordersides.None :
                    
                    break;
            }
            
          
        }

    
        private void cstTextBox_KeyUp(object sender, KeyEventArgs e)
        {

        }

      

        private void baseTextBox_GotFocus(object sender, EventArgs e)
        {
                this.OnGotFocus(e);
        
        }

        private void baseTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.OnKeyPress(e);
        }

        private void baseTextBox_TextChanged(object sender, EventArgs e)
        {
            if (OnTextChange != null)
            {
                OnTextChange(this,e);
            }
        }

        private void baseTextBox_LostFocus(object sender, EventArgs e)
        {
            
            this.OnLostFocus(e);

        }

        private void baseTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            this.OnKeyUp(e);
        }

        private void baseTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            this.OnKeyDown(e);
            
        }
       
       

    }
   
}